<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/12/4
 * Time: 15:00
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Sign extends Base
{
    /**
     * 首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('sign_draw')->count();// 获取总条数
            $lists = Db::name('sign_draw')
                ->field('sd_id, sd_sign_num, sd_draw_num, sd_time, sd_updatetime')
                ->order('sd_time desc')
                ->page($NowPage, $limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function signAdd(){
        if(Request::instance()->isPost()){
            $sd_sign_num = trim(input('sd_sign_num'), ' ');
            $sd_draw_num = trim(input('sd_draw_num'), ' ');
            if($sd_sign_num == 1) return json(['code' => 100, 'msg' => '连续签到天数不能为1天']);
            if($sd_sign_num > 30) return json(['code' => 100, 'msg' => '连续签到天数不可超过30天']);
            $exist = Db::table('up_sign_draw')
                ->where('sd_sign_num', $sd_sign_num)
                ->find();
            if($exist) return json(['code' => 100, 'msg' => '连续签到天数重复']);
            $res = Db::table('up_sign_draw')
                ->insert([
                    'sd_sign_num' => $sd_sign_num,
                    'sd_draw_num' => $sd_draw_num,
                    'sd_time' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('sign/signadd');
    }
    
    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function signEdit(){
        $sd_id = input('sd_id');
        if(Request::instance()->isPost()){
            $sd_sign_num = trim(input('sd_sign_num'), ' ');
            $sd_draw_num = trim(input('sd_draw_num'), ' ');
//            if($sd_sign_num == 1) return json(['code' => 100, 'msg' => '连续签到天数不能为1天']);
//            if($sd_sign_num > 30) return json(['code' => 100, 'msg' => '连续签到天数不可超过30天']);
//            $exist = Db::table('up_sign_draw')
//                ->where('sd_sign_num', $sd_sign_num)
//                ->where('sd_id', 'NEQ', $sd_id)
//                ->find();
//            if($exist) return json(['code' => 100, 'msg' => '连续签到天数重复']);
            $res = Db::table('up_sign_draw')
                ->where('sd_id', $sd_id)
                ->update([
                    'sd_sign_num' => $sd_sign_num,
                    'sd_draw_num' => $sd_draw_num,
                    'sd_updatetime' => date('Y-m-d H:i:s')
                ]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $sign = Db::table('up_sign_draw')->where('sd_id', $sd_id)->field('sd_sign_num, sd_draw_num')->find();
        $this->assign([
            'sd_id' => $sd_id,
            'sign' => $sign
        ]);
        return $this->fetch('sign/signedit');
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function signDel(){
        $sd_id = input('id');
        $res = Db::table('up_sign_draw')->delete($sd_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}